import requests
import sys
import json

url = "https://spark-api-open.xf-yun.com/v1/chat/completions"
model = "4.0Ultra"
APIpassword = sys.argv[1]

question = sys.argv[2]

data = {
        "model": model,
        "messages": [
            {
                "role": "user",
                "content": question
            }
        ],
        "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "返回实时天气",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "河北省承德市双桥区",
                        },
                        "format": {
                            "type": "string",
                            "enum": ["celsius", "fahrenheit"],
                            "description": "使用本地区常用的温度单位计量",
                        },
                    },
                    "required": ["location", "format"],
                }
            }
        }
    ]
    }
header = {
    "Authorization": "Bearer "+APIpassword
}
response = requests.post(url, headers=header, json=data)


try:
    data = json.loads(response.text)
    if data['code']==1:
        sys.exit()
except json.JSONDecodeError as e:
    pass

try:
    data = json.loads(response.text)
    content = data['choices'][0]['message']['content']
    print(content)
except json.JSONDecodeError as e:
    pass
